/*
 * jlb
 */
package com.jlb.user.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.jlb.core.base.BaseDAO;
import com.jlb.core.base.BaseSVImpl;
import com.jlb.core.entity.BeanRet;
import com.jlb.core.entity.Page;
import com.jlb.core.exceptions.BaseException;
import com.jlb.core.exceptions.UserException;
import com.jlb.core.exceptions.UserJlbException;
import com.jlb.core.tools.DateTools;
import com.jlb.core.tools.redis.RedisKey;
import com.jlb.core.tools.redis.RedisUtils;
import com.jlb.core.tools.security.Md5;
import com.jlb.my.dao.MyGroupDAO;
import com.jlb.my.entity.MyGroup;
import com.jlb.platform.entity.PlatformAccount;
import com.jlb.setting.dao.SettingDAO;
import com.jlb.setting.entity.Setting;
import com.jlb.star.dao.StarLevelDAO;
import com.jlb.star.dao.StarRecordDAO;
import com.jlb.star.entity.StarLevel;
import com.jlb.star.entity.StarRecord;
import com.jlb.user.dao.*;
import com.jlb.user.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userSV")
@Slf4j
public class UserSVImpl extends BaseSVImpl<User, Long> implements UserSV {

    @Autowired
    private UserDepositDAO userDepositDAO;

    @Autowired
    private UserOreDAO userOreDAO;

    @Autowired
    private UserJlbDAO userJlbDAO;

    @Autowired
    private StarLevelDAO starLevelDAO;

    @Autowired
    private StarRecordDAO starRecordDAO;

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private UserGroupDAO userGroupDAO;

    @Autowired
    private MyGroupDAO myGroupDAO;


    @Autowired
    private SettingDAO settingDAO;

    @Autowired
    private UserSubAccountDAO userSubAccountDAO;

    @Resource
    private RedisUtils redisUtils;


    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return userDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(User entity) throws BaseException {
        entity.setCode(String.valueOf(uidGenerator.getUID()));

        entity.setStar(0L);
        entity.setState(UserState.Active.name());
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        super.save(entity);
    }

    /**
     * 加载一个对象User
     *
     * @param id * @param code 编码
     * @return User
     */
    @Override
    public User load(java.lang.Long id, java.lang.String code) {
        if (id == null && code == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return userDAO.load(param);
    }

    /**
     * 加载一个对象User 通过id
     *
     * @param id
     * @return User
     */
    @Override
    public User loadById(java.lang.Long id) {
        if (id == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return userDAO.loadById(id);
    }

    /**
     * 加载一个对象User 通过code
     *
     * @param code 编码
     * @return User
     */
    @Override
    public User loadByCode(java.lang.String code) {
        if (code == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return userDAO.loadByCode(code);
    }

    @Override
    public User loadByCodeNotRelation(String code) {
        return userDAO.loadByCodeNotRelation(code);
    }

    @Override
    public User loadByAccount(String account) {
        if (account == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        Map<String, Object> params = new HashedMap();
        params.put("account", account);
        return userDAO.load(params);
    }

    /**
     * 获取用户信息 通过userCode
     * <p>
     * 1.获取用户的基本信息
     * 2.获取用户的直推人数，获取用户的团队人数
     * 3.获取用户押金
     * 4.获取用户的金兰宝
     * 5.获取用户的矿石
     * 6.获取用户的激活码数量
     *
     * @param userCode 编码
     * @return User
     */
    @Override
    public BeanRet getUserInfo(String userCode) {
        User user = this.loadByCode(userCode);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("user", user);
        //2.获取用户的直推人数
        Map<String, Object> params = new HashedMap();
        params.put("preUserCode", userCode);
        Integer invite = userGroupDAO.count(params);
        jsonObject.put("invite", invite != null ? invite : 0);
        //获取用户的团队人数
        jsonObject.put("group", user);
        jsonObject.put("deposit", user);
        jsonObject.put("jlb", user);
        jsonObject.put("ore", user);
        jsonObject.put("activeNum", user);

        return BeanRet.create(true, "查询成功", jsonObject);
    }


    /**
     * 根据主键id,oldStates 共同更新 User 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(java.lang.Long id, UserState newState, UserState... oldStates) {
        if (id == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        userDAO.updateStateById(id, new Date(), newState, oldStates);
    }

    /**
     * 根据主键code,oldStates 共同更新 User 的状态到newState状态
     *
     * @param code      编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByCode(java.lang.String code, UserState newState, UserState... oldStates) {
        if (code == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        userDAO.updateStateByCode(code, new Date(), newState, oldStates);
    }

    /**
     * 根据主键id 更新 User 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(java.lang.Long id, UserState state) {
        if (id == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        userDAO.updateById(id, state, new Date());
    }

    /**
     * 根据主键code 更新 User 的状态到另一个状态
     *
     * @param code  编码
     * @param state 状态
     */
    @Override
    public void updateByCode(java.lang.String code, UserState state) {
        if (code == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        userDAO.updateByCode(code, state, new Date());
    }


    /**
     * 删除对象User
     *
     * @param id * @param code 编码
     * @return User
     */
    @Override
    public void delete(java.lang.Long id, java.lang.String code) {
        if (id == null && code == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        userDAO.delete(param);
    }


    /**
     * 查询User分页
     *
     * @param user   对象
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<User>
     */
    @Override
    public List<User> list(User user, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (user != null) {
            map = JSON.parseObject(JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return userDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(User user) {
        Map<String, Object> map = null;
        if (user != null) {
            map = JSON.parseObject(JSON.toJSONString(user, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return userDAO.count(map);
    }

    /**
     * 查询User分页
     *
     * @param id     * @param code  编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<User>
     */
    @Override
    public List<User> list(java.lang.Long id, java.lang.String code, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return userDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id, java.lang.String code) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return userDAO.count(param);
    }

    /**
     * 注册
     * 0.添加全局锁，防止用户重复注册
     * 1.检查推荐人是否存在
     * 2.检查账户是否重复
     * 3.创建新用户
     * 4.创建推荐人的推荐关系
     * 5.推荐人进行升级并扣除押金，如果扣成负值则停止动态收益，金兰宝充足再恢复
     * 6.创建新人与上层23层的关系
     * 7.初始化注册人矿石账户
     *
     * @param account          账户
     * @param password         密码
     * @param pinPassword      支付密码
     * @param realName         真实姓名
     * @param phone            手机号
     * @param referenceAccount 推荐账户
     */
    @Override
    public void reg(String account, String realName, String password, String pinPassword, String phone, String referenceAccount) {
        if (StringUtils.isBlank(account) || StringUtils.isBlank(password) || StringUtils.isBlank(pinPassword)
                || StringUtils.isBlank(phone) || StringUtils.isBlank(referenceAccount)) {
            throw new UserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        //0.添加全局缓存锁，防止用户重复注册
        String md5 = Md5.md5(account + realName + password + pinPassword + phone + referenceAccount);
        String exist = (String)redisUtils.get(md5);
        if(StringUtils.isNotEmpty(exist)) {
            throw new UserException(BaseException.BaseExceptionEnum.User_Is_Exist);
        }
        redisUtils.set(RedisKey.genReg(md5), account);

        // 1.检查推荐人是否存在
        Map<String, Object> param = new HashMap<>();
        param.put("account", referenceAccount);
        param.put("state", UserState.Active.name());
        User referenceUser = userDAO.load(param);
        if (referenceUser == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Result_Not_Exist);
        }

        // 2.检查账户是否重复
        param.clear();
        param.put("account", account);
        User loadUser = userDAO.load(param);
        if (loadUser != null) {
            throw new UserException(BaseException.BaseExceptionEnum.User_Is_Exist);
        }

        // 3.创建新用户
        User user = new User(account, Md5.md5(password), Md5.md5(pinPassword), phone);
        user.setCode(String.valueOf(uidGenerator.getUID()));
        user.setStar(0L);
        user.setState(UserState.Active.name());
        user.setLeader(UserLeaderEnum.Member.name());
        user.setRealName(realName);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userDAO.insert(user);

        // 4.创建与推荐人的关系
        UserGroup userGroup = new UserGroup(user.getCode(), referenceUser.getCode());
        userGroupDAO.insert(userGroup);


        //5.推荐人进行升级并扣除押金，如果扣成负值则停止动态收益，金兰宝充足再恢复
        //获取最大星级限制
        int starUpperLimit = Integer.parseInt(settingDAO.loadByK(Setting.Key.Star_Upper_Limit.name()).getV());
        if (referenceUser.getStar() < starUpperLimit) {
            referenceUser.setStar(referenceUser.getStar() + 1);
            String summary = "";
            //扣除升级押金
            param.clear();
            param.put("starLevel", String.valueOf(referenceUser.getStar()));
            StarLevel starLevel = starLevelDAO.load(param);
            String deposit = starLevel.getDeposit();
            summary = "推广用户[" + account + "]进行星级升级，自动扣除升级押金[" + deposit + "]";
            if (!"0".equals(deposit)) {
                param.clear();
                param.put("userCode", referenceUser.getCode());
                UserJlb userJlb = userJlbDAO.load(param);
                if (userJlb == null || UserJlbState.Frozen == UserJlbState.getEnum(userJlb.getState())) {
                    throw new UserJlbException(BaseException.BaseExceptionEnum.Invite_Jlb_Not_Enough_Error);
                }

                userJlb.setJlb(userJlb.getJlb() - Integer.parseInt(deposit));
                userJlbDAO.update(userJlb);

                //金兰宝不充足则扣除后自动停止动态矿石收益
                if (userJlb.getJlb() < 0) {
                    UserOre userOre = userOreDAO.loadByUserCode(referenceUser.getCode());
                    if (userOre == null) {
                        userOre = new UserOre().init(referenceUser.getCode());
                        userOreDAO.insert(userOre);
                    }

                    if (UserOreState.Active == UserOreState.getEnum(userOre.getState())) {
                        userOre.setState(UserOreState.Stop.name());
                        userOreDAO.update(userOre);
                    }
                    summary = summary + ",由于金兰宝不足已经暂停了动态收益,金兰宝足够时将自动扣除恢复";

                }

                //增加扣除押金的记录
                userDepositDAO.insert(new UserDeposit(referenceUser.getCode(), Integer.parseInt(deposit),
                        referenceUser.getStar().intValue(), UserDepositState.Frozen.name(), DateTools.calculateByYear(new Date(), 1)));
            }

            //记录星级升级日志
            starRecordDAO.insert(new StarRecord(referenceUser.getCode(), summary,
                    Integer.parseInt(String.valueOf((referenceUser.getStar() - 1))), Integer.parseInt(String.valueOf(referenceUser.getStar()))));
        }
        referenceUser.setUpdateTime(new Date());
        userDAO.update(referenceUser); //升级更新


        // 6.创建新人与上层23层的关系
        this.layersRelationship(user.getCode());

        //7.初始化注册人矿石账户
        userOreDAO.insert(new UserOre().init(user.getCode()));
        //初始化金兰宝账户
        UserJlb userJlb = new UserJlb();
        userJlb.setCode(String.valueOf(uidGenerator.getUID()));
        userJlb.setUserCode(user.getCode());
        userJlb.setJlb(0);
        userJlb.setRelease(0);
        userJlb.setState(UserJlbState.Active.name());
        userJlb.setDigInTotalJlb(0);
        userJlb.setDigInJlb("0");
        userJlb.setDigInJlbState(UserJlbMinerState.Dig_In.name());
        userJlb.setMallTotalJlb(0);
        userJlbDAO.insert(userJlb);
    }


    /**
     * 迭代系统限制层级数，将每一层与新增的用户建立１：１层级关系
     * 如：Ｃ　是新用户　Ｂ是推荐人　那么就有　Ｃ是Ｂ的第１层，是Ａ的第２层以此类推向上２３层或者Ｎ层的
     * １对１关系
     *
     * @param newUserCode 　新用户编码
     */
    private void layersRelationship(String newUserCode) {
        //查询层级最大限制
        int layers = Integer.parseInt(settingDAO.loadByK(Setting.Key.Layers.name()).getV());

        Map<String, Object> param = new HashMap<>();
        param.put("userCode", newUserCode);
        UserGroup userGroup = userGroupDAO.load(param);
        //获得推荐人的上级用户编码
        for (int layer = 1; layer <= layers; layer++) {
            if (userGroup != null) {
                MyGroup myGroup = new MyGroup(userGroup.getPreUserCode(), newUserCode, layer);
                myGroupDAO.insert(myGroup);
            }
            param.clear();
            if (userGroup == null) {
                break;
            }
            if (userGroup.getPreUserCode().equals(PlatformAccount.PlatformAccountEnum.Platform.name())) {
                break;
            }
            param.put("userCode", userGroup.getPreUserCode());
            userGroup = userGroupDAO.load(param);
        }
    }

    /**
     * 关联子账户：关联者需是被关联者的直接推荐者
     * 1.账户是否存在
     * 2.关联者是否是被关联者的推荐人
     * 3.进行关联
     *
     * @param userCode 关联者用户编码
     * @param account  被关联者账户
     * @param password
     */
    @Override
    public void bindAccount(String userCode, String account, String password) {
        if (StringUtils.isEmpty(userCode) || StringUtils.isEmpty(account)) {
            throw new UserException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        //1.账户是否存在
        User fatherUser = userDAO.loadByCode(userCode);
        if (fatherUser == null) {
            throw new UserException(BaseException.BaseExceptionEnum.User_Error);
        }
        User childrenUser = userDAO.loadByAccount(account);
        if (childrenUser == null) {
            throw new UserException(BaseException.BaseExceptionEnum.User_Error);
        }
        //被关联子账户密码是否正确
        if (!Md5.md5(password).equals(childrenUser.getPassword())) {
            throw new UserException(BaseException.BaseExceptionEnum.Account_Error);
        }
        Map<String, Object> params = new HashedMap();
        params.put("userCode", userCode);
        params.put("subUserCode", childrenUser.getCode());
        UserSubAccount userSubAccountFlag = userSubAccountDAO.load(params);
        if (userSubAccountFlag != null) {
            throw new UserException(BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        //2.关联者是否是被关联者的推荐人
        //业务修改为只要输入正确的被关联账户的账号和密码，既能关联成功
      /*  Map<String, Object> param = new HashMap<>();
        param.put("userCode", childrenUser.getCode());
        UserGroup userGroup = userGroupDAO.load(param);
        if (userGroup == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Not_Allow_Connection);
        }
        if (!userGroup.getPreUserCode().equals(fatherUser.getCode())) {
            throw new UserException(BaseException.BaseExceptionEnum.Can_Not_Connection);
        }*/
        //3.进行关联
        UserSubAccount userSubAccount = new UserSubAccount();
        userSubAccount.setUserCode(userCode);
        userSubAccount.setSubUserCode(childrenUser.getCode());
        userSubAccount.setCreateTime(new Date());
        userSubAccountDAO.insert(userSubAccount);


    }

    /**
     * 用户A,B间是否可以转账
     * 1.查询两个用户的直接领导人，两个用户的领导人相同，可以相互转账；两个用户的直接领导人不一致,则2
     * 2.判断两个用户是否是同一个链上的，即用户A的直接或间接推荐人是否有B，或B的直接或间接推荐人是否有A
     *
     * @param sendCode   转账人用户编码
     * @param acceptCode 接收者用户编码
     * @return boolean
     */
    @Override
    public boolean isAllowTranfer(String sendCode, String acceptCode) {
        //禁止领导人互转
        User sendUser = userDAO.loadByCodeNotRelation(sendCode);
        User acceptUser = userDAO.loadByCodeNotRelation(acceptCode);
        if ((UserLeaderEnum.getEnum(sendUser.getLeader()) == UserLeaderEnum.Leader) && (UserLeaderEnum.getEnum(acceptUser.getLeader()) == UserLeaderEnum.Leader)) {
            throw new UserJlbException(BaseException.BaseExceptionEnum.Not_Allow_Transfer);
        }
        if (((UserLeaderEnum.getEnum(sendUser.getLeader()) == UserLeaderEnum.Leader)
                && (UserLeaderEnum.getEnum(acceptUser.getLeader()) == UserLeaderEnum.AreaManager))
                || ((UserLeaderEnum.getEnum(sendUser.getLeader()) == UserLeaderEnum.AreaManager)
                && (UserLeaderEnum.getEnum(acceptUser.getLeader()) == UserLeaderEnum.Leader))) {
            return true;
        }

        User sendUserLeader = this.getLeader(sendCode);
        User acceptUserLeader = this.getLeader(acceptCode);
        //1.查询两个用户的直接领导人，两个用户的领导人相同，可以相互转账
        if (sendUserLeader == null || acceptUserLeader == null) {
            return false;
        }
        if (sendUserLeader != null && acceptUserLeader != null && sendUserLeader.getCode().equals(acceptUserLeader.getCode())) {
            return true;
        }


      /*  Map<String, Object> param = new HashMap<>();
        param.put("userCode", sendCode);
        param.put("subUserCode", acceptCode);
        MyGroup myGroup = myGroupDAO.load(param);
        if (myGroup == null) {
            param.clear();
            param.put("userCode", acceptCode);
            param.put("subUserCode", sendCode);
            myGroup = myGroupDAO.load(param);
            if (myGroup != null) {
                return true;
            }
        } else {
            log.debug("isAllowTranfer() :允许转账");
            return true;
        }
*/

//        TODO 同一个线上是否可以转账控制
//        //2.两个用户的直接领导人不一致，或者两个用户有一人或者都没有领导人，判断两个用户是否是同一个链上的，即用户A的直接或间接推荐人是否有B，或B的直接或间接推荐人是否有A
//        boolean sendBoolean = this.isChild(sendCode, acceptCode);
//        boolean acceptBoolean = this.isChild(acceptCode, sendCode);
//        if (sendBoolean || acceptBoolean) {
//            return true;
//        }
        return false;
    }

    @Override
    public void updateIdCard(String userCode, String idCard) {
        User user = userDAO.loadByCode(userCode);
        if (user == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Session_Error);
        }
        user.setIdCard(idCard);
        userDAO.update(user);
    }

    @Override
    public void updateAlipay(String userCode, String alipay) {
        User user = userDAO.loadByCode(userCode);
        if (user == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Session_Error);
        }
        user.setAlipay(alipay);
        userDAO.update(user);
    }

    @Override
    public void updateRealName(String userCode, String realName) {
        User user = userDAO.loadByCode(userCode);
        if (user == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Session_Error);
        }
        user.setRealName(realName);
        userDAO.update(user);
    }

    @Override
    public void updatePhone(String userCode, String phone) {
        User user = userDAO.loadByCode(userCode);
        if (user == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Session_Error);
        }
        user.setPhone(phone);
        userDAO.update(user);
    }

    @Override
    public void updateWechat(String userCode, String wechat) {
        User user = userDAO.loadByCode(userCode);
        if (user == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Session_Error);
        }
        user.setWechat(wechat);
        userDAO.update(user);
    }

    @Override
    public void updateNickName(String userCode, String nickName) {
        User user = userDAO.loadByCode(userCode);
        if (user == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Session_Error);
        }
        user.setNickName(nickName);
        userDAO.update(user);
    }

    @Override
    public void updatePassword(String userCode, String password) {
        User user = userDAO.loadByCode(userCode);
        if (user == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Session_Error);
        }
        password = Md5.md5(password);
        user.setPassword(password);
        userDAO.update(user);
    }

    @Override
    public void updatePinPassword(String userCode, String pinPassword) {
        User user = userDAO.loadByCode(userCode);
        if (user == null) {
            throw new UserException(BaseException.BaseExceptionEnum.Session_Error);
        }
        pinPassword = Md5.md5(pinPassword);
        user.setPinPassword(pinPassword);
        userDAO.update(user);
    }

    /**
     * 子账户转账
     *
     * @param userCode    父账户编码
     * @param subAccount  转账子账户信息
     * @param accoun      要转的账户信息
     * @param jlb         转的金兰宝数量
     * @param ore         转的矿石数量
     * @param active      转的激活码数量
     * @param pinPassword 支付密码
     */
    @Override
    public void transferSubAccount(String userCode, String subAccount, String accoun, int jlb, int ore, int active, String pinPassword) {

    }

    /**
     * 查询今日新增会员量
     *
     * @return
     */
    @Override
    public int countToday() {
        return userDAO.countToday();
    }

    @Override
    public User loadByAccountForCreateSql(String account) {
        return userDAO.loadByAccountForCreateSql(account);
    }

    /**
     * 查询某个用户的领导人
     *
     * @param userCode 用户编码
     * @return
     */
    private User getLeader(String userCode) {
        User user = userDAO.loadByCodeNotRelation(userCode);
        if (user != null && (UserLeaderEnum.getEnum(user.getLeader()) == UserLeaderEnum.Leader)) {
            return user;
        }
        UserGroup userGroup;
        while (true) {
            userGroup = userGroupDAO.loadByUserCode(userCode);
            if (userGroup == null || userGroup.getPreUserCode().equals(PlatformAccount.PlatformAccountEnum.Platform.name())) {
                break;
            }
            user = userDAO.loadByCodeNotRelation(userGroup.getPreUserCode());
            if (UserLeaderEnum.Leader == UserLeaderEnum.getEnum(user.getLeader())) {
                break;
            } else {
                userCode = userGroup.getPreUserCode();
            }
        }
        //如果最后一个用户还不是领导人，则该用户没有领导人
        if (user != null && UserLeaderEnum.Leader != UserLeaderEnum.getEnum(user.getLeader())) {
            user = null;
        }
        return user;
    }

    /**
     * 查询用户A是否是B的直接或间接子节点
     *
     * @param childUserCode
     * @param fatherUserCode
     * @return
     */
    private boolean isChild(String childUserCode, String fatherUserCode) {
        boolean groupIsInclude = false;
        UserGroup userGroup;
        User user;
        while (true) {
            userGroup = userGroupDAO.loadByUserCode(childUserCode);
            if (userGroup == null || userGroup.getPreUserCode().equals(PlatformAccount.PlatformAccountEnum.Platform.name())) {
                break;
            }
            user = userDAO.loadByCode(userGroup.getPreUserCode());
            if (user.getCode().equals(fatherUserCode)) {
                groupIsInclude = true;
                break;
            } else {
                childUserCode = userGroup.getPreUserCode();
            }
        }
        return groupIsInclude;
    }


}
